home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / TODOLIST.ARJ / TODOWIN.H < prev    next >
C/C++ Source or Header  |  1991-11-11  |  10KB  |  254 lines

  1. #if !defined( __TODOWIN_H )
  2. #define __TODOWIN_H
  3.  
  4. //---------------------------------------------------------------------
  5. //
  6. //  TODOWIN.H
  7. //
  8. //      Copyright (c) 1991 by Borland International
  9. //      All Rights Reserved.
  10. //
  11. //  Defines the following classes, which are useful for building
  12. //  general purpose windows applications:
  13. //
  14. //  WinBase - provides basic data and functionality for
  15. //            windows programming.
  16. //
  17. //  ModalDialog - provides for modal dialog boxes.  Inherits from
  18. //            WinBase, which is a virtual base.
  19. //
  20. //  Window  - provides core functionality for a window under Windows.
  21. //            Inherits from WinBase, which is a virtual base.
  22. //
  23. //---------------------------------------------------------------------
  24.  
  25. #if !defined( __WINDOWS_H )
  26. #include <Windows.h>
  27. #endif  // __WINDOWS_H
  28.  
  29. #if !defined( __ASSERT_H )
  30. #include <assert.h>
  31. #endif  // __ASSERT_H
  32.  
  33. //---------------------------------------------------------------------
  34. //
  35. //  class WinBase
  36. //
  37. //      provides the basic data and functionality for all classes
  38. //      used in the windows application.  It is an abstract base class.
  39. //
  40. //---------------------------------------------------------------------
  41.  
  42. class WinBase
  43. {
  44.  
  45. public:
  46.  
  47.     virtual WORD run() = 0;     // the core function of all windows!  For
  48.                                 // the main application window, this
  49.                                 // provides the message loop.  In modal
  50.                                 // dialogs, it sets up the dialog box,
  51.                                 // calls the dialog proc, and closes down
  52.                                 // the dialog.
  53.  
  54.  
  55.  
  56.     static  HANDLE hInst;       // the handle of the current instance
  57.  
  58.     static  HANDLE hPrevInst;   // the handle of the previous instance
  59.  
  60.     static  LPSTR cmd;          // pointer to the command line
  61.  
  62.     static  int show;           // the nCmdShow parameter of the current
  63.                                 // instance
  64.  
  65.     HANDLE  hWnd();             // access function
  66.  
  67. protected:
  68.  
  69.     HWND hWindow;               // the window handle of the class.  This is
  70.                                 // accessed through hWnd(), and it will
  71.                                 // provide the correct handle for any
  72.                                 // derived class.
  73.  
  74.                                 // NOTE: this field is not initialized by
  75.                                 // the constructor of this class.  It must
  76.                                 // be initialized by the constructor of a
  77.                                 // class derived from this class.
  78.  
  79. };
  80.  
  81. //---------------------------------------------------------------------
  82. //
  83. //  class ModalDialog
  84. //
  85. //      provides basic functionality for modal dialog boxes.  It is an
  86. //      abstract base class.
  87. //
  88. //---------------------------------------------------------------------
  89.  
  90. class ModalDialog : public virtual WinBase
  91. {
  92.  
  93. public:
  94.  
  95.     ModalDialog( HWND );        // constructor.  Since a modal dialog
  96.                                 // needs to know its owner, the handle
  97.                                 // of the owner is passed as a parameter
  98.                                 // to the constructor.
  99.  
  100.     ~ModalDialog();
  101.  
  102.     virtual WORD run();         // the core of a modal dialog!  It sets
  103.                                 // up the dialog, executes it, and closes
  104.                                 // it down.
  105.  
  106. protected:
  107.  
  108.     virtual BOOL dispatch( HWND, WORD, WORD, LONG );
  109.                                 // the core of any window.  Whenever
  110.                                 // dlgProc receives a message it passes
  111.                                 // the message on to dispatch().  If
  112.                                 // dispatch() doesn't handle the message
  113.                                 // itself, it should call the dispatch()
  114.                                 // function in its base class.  Ultimately,
  115.                                 // messages not handled higher up are
  116.                                 // handled by this version of dispatch(),
  117.                                 // which just returns FALSE, indicating
  118.                                 // that the message wasn't handled by
  119.                                 // the dialog box at all.
  120.  
  121.             WORD result;        // this holds the value which will be
  122.                                 // returned by run().  If the dialog box
  123.                                 // needs to return a success code, that
  124.                                 // code should be stored here by the
  125.                                 // dialog handler.
  126.  
  127. private:
  128.  
  129.     virtual LPSTR getDialogName() = 0;
  130.                                 // returns a far pointer to a string
  131.                                 // that contains the name of the dialog
  132.                                 // resource for the current dialog box.
  133.                                 // This is used in run() to load the
  134.                                 // resource.
  135.  
  136.     static  BOOL FAR PASCAL _export dlgProc( HWND, WORD, WORD, LONG );
  137.                                 // the dialog proc that windows calls
  138.                                 // when it has messages for the current
  139.                                 // dialog.
  140.  
  141.     static  ModalDialog *curDlg;
  142.                                 // this holds a pointer to the currenly
  143.                                 // active ModalDialog.  It is set up when
  144.                                 // the constructor is called, and is used
  145.                                 // by dlgProc() to route messages to the
  146.                                 // right dispatch() function.
  147.  
  148. };
  149.  
  150. //---------------------------------------------------------------------
  151. //
  152. //  class Window
  153. //
  154. //      provides the basic functionality for a normal window under
  155. //      windows.
  156. //
  157. //---------------------------------------------------------------------
  158.  
  159. class Window : public virtual WinBase
  160. {
  161.  
  162. public:
  163.  
  164.     virtual BOOL create();      // creates the window.  This involves
  165.                                 // registering the window class if
  166.                                 // it hasn't already been registered,
  167.                                 // creating the actual window, and
  168.                                 // inserting the window into the internal
  169.                                 // window list.
  170.  
  171.     virtual WORD run();         // provides the message loop.
  172.  
  173. protected:
  174.  
  175.     virtual LONG dispatch( WORD, WORD, LONG );
  176.                                 // dispatches messages in a class derived
  177.                                 // from Window.  When wndProc() receives
  178.                                 // a message, it determines which Window
  179.                                 // object the message should be routed
  180.                                 // to, and calls dispatch() for that object.
  181.                                 // If the dispatch() function in a derived
  182.                                 // class does not handle any particular
  183.                                 // message, it should pass that message
  184.                                 // on down to the dispatch() function in
  185.                                 // its base class.  The version of dispatch()
  186.                                 // in Window itself just calls DefWindowProc.
  187.  
  188.     virtual BOOL registerClass() = 0;
  189.                                 // the derived class should override this
  190.                                 // function and register a window class
  191.                                 // defined appropriately for the program.
  192.  
  193.     virtual BOOL createWindow() = 0;
  194.                                 // the derived class should override this
  195.                                 // function and create a window that's
  196.                                 // appropriate for the program.
  197.  
  198.     static  LONG FAR PASCAL _export wndProc( HWND, WORD, WORD, LONG );
  199.                                 // the window proc that windows calls
  200.                                 // when it has messages for any window
  201.                                 // in the current application.  wndProc()
  202.                                 // posts the message to the appropriate
  203.                                 // window.
  204.  
  205.             void insert();      // should be called from createWindow()
  206.                                 // after successfully calling the windows
  207.                                 // function CreateWindow().  insert() adds
  208.                                 // the current object to the Window list,
  209.                                 // enabling normal dispatching of messages
  210.                                 // to the current object.
  211.  
  212. private:
  213.  
  214.     static Window *winList;     // have to maintain a list of Windows so
  215.     Window *nextWin;            // that we can dispatch messages to the
  216.                                 // correct one.
  217.  
  218.     static Window *inCreate;    // sort-of kludgy.  But there are messages
  219.                                 // that are sent to a window during the
  220.                                 // call to CreateWindow().  We assume that
  221.                                 // any message received by wndProc() during
  222.                                 // a call to CreateWindow() is meant for
  223.                                 // the window being created, and dispatch
  224.                                 // those messages to that window.
  225.  
  226. };
  227.  
  228. inline HANDLE WinBase::hWnd()
  229. {
  230.     return hWindow;
  231. }
  232.  
  233. inline ModalDialog::ModalDialog( HWND hOwner ) : result( 0 )
  234. {
  235.     assert( curDlg == 0 );
  236.     curDlg = this;
  237.     hWindow = hOwner;
  238. }
  239.  
  240. inline ModalDialog::~ModalDialog()
  241. {
  242.     assert( curDlg != 0 );
  243.     curDlg = 0;
  244. }
  245.  
  246. inline void Window::insert()
  247. {
  248.     nextWin = winList;
  249.     winList = this;
  250. }
  251.  
  252. #endif  // __TODOWIN_H
  253.  
  254.